package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"missC/model"
	"missC/utils/er"
	"strconv"
)

// InsArt 添加文章
func InsArt(c *gin.Context) {
	var formData model.Article
	_ = c.ShouldBindJSON(&formData)

	var code int
	//添加文章
	code = model.InsArt(&formData)
	c.JSON(200, gin.H{
		"code": code,
		"data": formData,
		"msg":  er.GetErr(code),
	})

}

// GetArt 查询所有文章
func GetArt(c *gin.Context) {
	//页数
	page, _ := strconv.Atoi(c.Query("page"))
	if page == 0 {
		page = 1
	}
	//行数
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	formData, code, total := model.GetArt(pageSize, page)
	c.JSON(200, gin.H{
		"code":  code,
		"data":  formData,
		"total": total,
		"msg":   er.GetErr(code),
	})
}

// DelArt 删除接口
func DelArt(c *gin.Context) {
	var formData model.Article
	_ = c.ShouldBindJSON(&formData)

	var code int
	fmt.Println(formData.Id)
	code = model.DelArt(formData.Id)
	c.JSON(200, gin.H{
		"code": code,
		"data": formData.Id,
		"msg":  er.GetErr(code),
	})
}

//更新文章接口

func UpArt(c *gin.Context) {
	var formData model.Article
	_ = c.ShouldBindJSON(&formData)

	var code int
	fmt.Println(formData)
	code = model.UpArt(&formData)
	c.JSON(200, gin.H{
		"code": code,
		"data": formData.Id,
		"msg":  er.GetErr(code),
	})
}
